package com.example.statistics.ui.sales

import android.os.Bundle
import com.example.statistics.R
import com.example.statistics.bean.SalesProduct
import com.example.statistics.common.BaseActivity
import com.example.statistics.ex.goTimeFormat
import com.example.statistics.ex.httpGet
import com.example.statistics.ex.slimAdapter
import com.example.statistics.ex.tell
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.Constants
import com.jhj.slimadapter.itemdecoration.LineItemDecoration
import com.example.statistics.ex.contentText
import kotlinx.android.synthetic.main.activity_customer_info.*
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.toast

class SalesCustomerInfoActivity : BaseActivity() {
    override val title: String
        get() = "客户"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_customer_info)

        recycler_view.addItemDecoration(LineItemDecoration())
        val adapter = slimAdapter<SalesProduct>(R.layout.llist_item_sales_customer) {
            text(R.id.tv_name, it.factoryNo + it.productName)
            text(R.id.tv_number, "${it.salesNumber}件 * ￥${it.salesPrice}")
            text(R.id.tv_date, it.date.goTimeFormat())
        }.attachTo(recycler_view)

        val id = intent.getIntExtra(Constants.ID, 0) ?: 0
        if (id == 0) {
            toast("请选择客户")
            return
        }
        tv_phone.onClick {
            tell(tv_phone.contentText.orEmpty())
        }
        httpGet<com.example.statistics.bean.Customer>(UrlConfig.SALES_CUSTOMER_DATA, config = {
            isShowDialog = true
            listParams = listOf("id" to id)
        }) {
            if (it != null) {
                tv_name.text = it.name?.replace("?", "")?.replace("？", "")
                tv_phone.text = it.phoneOne
                val product = it.productList?.map { it.name}?.joinToString("     ")
                tv_product.text = product
                if (!it.salesProductList.isNullOrEmpty()) {
                    adapter.setDataList(it.salesProductList.orEmpty())
                }
            }

        }
    }
}